package com.zhongjian.technologycal.mall.activity.personal.fragment;

import android.os.Bundle;
import android.widget.ListView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.adapter.WaitAppraiseOrderAdapter;
import com.zhongjian.technologycal.mall.base.BaseFragment;
import com.zhongjian.technologycal.mall.bean.OrderBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingTip;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/15.
 * 待评价的订单列表
 */

public class WaitAppraiseOrderListFragment extends BaseFragment {
    public static final String CATEGORY_NAME = "com.nanchen.aiyagirl.module.category.CategoryFragment.CATEGORY_NAME";
    @BindView(R.id.appraise_lv_order_listView)
    ListView appraise_lv_order_listView;
    @BindView(R.id.loadedTip)
    LoadingTip loadingTip;
    @BindView(R.id.appraise_refreshlayout)
    RefreshLayout refreshLayout;
    private WaitAppraiseOrderAdapter waitAppraiseOrderAdapter;
    private List<OrderBean> listItems = new ArrayList<>();
    private int page = 0;
    private int pageNum =20;

    @Override
    protected void initView() {
//      refreshLayout.autoRefresh();
        initRequest();
        waitAppraiseOrderAdapter = new WaitAppraiseOrderAdapter(getActivity(), listItems);
        appraise_lv_order_listView.setAdapter(waitAppraiseOrderAdapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {

                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page =0;
                        initRequest();
                        refreshlayout.finishRefresh();
                    }
                }, 1000);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(final RefreshLayout refreshlayout) {
                page++;
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        initRequest();
                        refreshlayout.finishLoadmore();
                    }
                }, 1000);
            }
        });

    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_wait_appraise_list;
    }


    public static WaitAppraiseOrderListFragment newInstance(String mCategoryName) {
        WaitAppraiseOrderListFragment categoryFragment = new WaitAppraiseOrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CATEGORY_NAME, mCategoryName);
        categoryFragment.setArguments(bundle);
        return categoryFragment;
    }

    @Override
    public void initPresenter() {
       initRequest();
    }

   private void initRequest(){
       if(!NetWorkUtils.isNetConnected(getActivity())){
           loadingTip.setLoadingTip(LoadingTip.LoadStatus.error);
           return;
       }
       HttpRetrofitClient.getInstance(getActivity()).getUrlRequestOrderList(UserUtils.getUserToken(),
               "wc",page,pageNum, new Subscriber<ResponseBody>() {
           @Override
           public void onStart() {
               super.onStart();
               loadingTip.setLoadingTip(LoadingTip.LoadStatus.loading);
           }

           @Override
           public void onCompleted() {
               if(listItems.size() <= 0){
                   loadingTip.setLoadingTip(LoadingTip.LoadStatus.empty);
               }

           }

           @Override
           public void onError(Throwable e) {
               loadingTip.setLoadingTip(LoadingTip.LoadStatus.sereverError);

           }

           @Override
           public void onNext(ResponseBody responseBody) {
               loadingTip.setLoadingTip(LoadingTip.LoadStatus.finish);
               try {
                   Gson gson = new Gson();
                   OrderBean orderBean = gson.fromJson(responseBody.string(), OrderBean.class);
                   List<OrderBean> forList = new ArrayList<>();
                   for (int i = 0; i < orderBean.getData().size(); i++) {
                       forList.add(orderBean);
                   }
                   setData(forList);

               } catch (Exception e) {
                   e.printStackTrace();
               }


           }
       });
    }

    private void setData(List<OrderBean> foreshowList) {
        if(page ==0){
            listItems.clear();
        }
        for (OrderBean vo : foreshowList) {
            listItems.add(vo);
        }
        waitAppraiseOrderAdapter.notifyDataSetChanged();

    }
}
